class Buff_State
  attr_reader   :actor
  attr_accessor   :buffs
  def initialize(actor)
    @actor = actor
    @buffs = []
  end
  def on_turn_start
    remove_items = []
    for item in @buffs
      item.on_turn_start
      if item.left_turn_count == 0
        remove_items.push(item.id)
      end
    end
    for item in remove_items
      remove_buff(item)
    end
  end
  def on_turn_end
    remove_items = []
    for item in @buffs
      item.on_turn_end
      if item.left_turn_count == 0
        remove_items.push(item.id)
      end
    end
    for item in remove_items
      remove_buff(item)
    end
  end
  def make_buff(id, turn_count)
    case id
      when 1
        return Buff_001.new(@actor, turn_count)
      when 2
        return Buff_002.new(@actor, turn_count)
      when 3
        return Buff_003.new(@actor, turn_count)
    end
  end
  def add_buff(id, turn_count)
    item = find_buff(id)
    if item == nil
      item = make_buff(id, turn_count)
      item.on_add
      @buffs.push(item)
    else
      item.change_left_count(turn_count)
    end
    @actor.add_popup_text(item.name + " " + item.left_turn_count.to_s, 1)
  end
  def remove_buff(id)
    for i in 0..@buffs.size
      item = @buffs[i]
      if item.id == id
        item.on_remove
        @actor.add_popup_text("-" + item.name, 1)
        @buffs.delete_at(i)
        break
      end
    end
  end
  def find_buff(id)
    res = nil
    for item in @buffs
      if item.id == id
        res = item
        break
      end
    end
    return res
  end
  def exist_buff(id)
    return find_buff(id) != nil
  end
  def left_turn_count(id)
    item = find_buff(id)
    return item == nil ? 0 : item.left_turn_count
  end
  def remove_all_battle_buff
    @buffs.clear
  end
end
